package com.example.exercise.mail.controller;

import com.example.exercise.common.BaseResponse;
import com.example.exercise.mail.service.EmailService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class EmailController {

    @Resource
    private EmailService emailService;

    @RequestMapping(value = "/sendSimpleEmail", method = {RequestMethod.POST})
    public BaseResponse<Object> sendSimpleEmail(@RequestParam String receiver) {
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        String subject = "主题:藜麦图书借阅系统注册成功通知";
        String text = "注册成功后请务必修改密码，每次借阅限期为一周，请及时归还；" +
                "若到期还需要继续使用，请登录图书借阅系统续借，祝您生活愉快，工作顺利";
        emailService.sendSimpleMail(receiver,subject,text);
        emailService.asyncDemo();
        baseResponse.ok();
        return baseResponse;
    }

    @RequestMapping(value = "/sendHtmlEmail", method = {RequestMethod.POST})
    public BaseResponse<Object> sendHtmlEmail(@RequestParam String receiver) {
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        String subject = "主题:藜麦图书借阅系统注册成功通知";
        String htmlText = "<font color='red'>注册成功后请务必修改密码，每次借阅限期为一周，请及时归还；" +
                "若到期还需要继续使用，请登录图书借阅系统续借，祝您生活愉快，工作顺利</font>";
        emailService.sendHtmlMail(receiver,subject,htmlText);
        baseResponse.ok();
        return baseResponse;
    }

    @RequestMapping(value = "/sendAttachmentEmail", method = {RequestMethod.POST})
    public BaseResponse<Object> sendAttachmentEmail(@RequestParam("receiver") String receiver, @RequestParam("file") MultipartFile multipartFile) {
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        String subject = "主题:藜麦图书借阅系统注册成功通知";
        String htmlText = "<font color='red'>注册成功后请务必修改密码，每次借阅限期为一周，请及时归还；" +
                "若到期还需要继续使用，请登录图书借阅系统续借，祝您生活愉快，工作顺利</font>";
        emailService.sendAttachMail(receiver,subject,htmlText,"E:\\img\\upload", new MultipartFile[]{multipartFile});
        baseResponse.ok();
        return baseResponse;
    }
}
